package ahblue.cn.tanzi.ui.person;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.mm.opensdk.modelmsg.SendAuth;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.TanZiApplication;
import ahblue.cn.tanzi.cache.ACache;
import ahblue.cn.tanzi.jmessage.JMessageManage;
import ahblue.cn.tanzi.jmessage.im.util.DialogCreator;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.model.user.UserModel;
import ahblue.cn.tanzi.ui.MainActivity;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 账号密码登陆
 * Created by fengdongfei on 2018/11/9.
 */

public class LoginActivity extends BaseActivity {

    @BindView(R.id.tvlogin)
    TextView tvlogin;
    @BindView(R.id.tvregist)
    TextView tvregist;
    @BindView(R.id.etname)
    EditText etname;
    @BindView(R.id.etpwd)
    EditText etpwd;
    @BindView(R.id.tvcode)
    TextView tvCode;
    private Dialog mDialog;

    public static void into(Context context) {
        context.startActivity(new Intent(context, LoginActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_pwd);
        ButterKnife.bind(this);
        // 进入登录界面就清楚本地数据
        JMessageManage.quit();
        ACache.get(this).clear();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (TanZiApplication.code != null) {
            //就在这个地方，用网络库什么的或者自己封的网络api，发请求去咯，注意是get请求
            LoginResultModel.WxLogin(this, TanZiApplication.code, new LoginResultModel.LoginStatus() {
                @Override
                public void conver(LoginResultModel logmodel) {
                    if (null != logmodel) {
                        UserModel.getUserInfo(LoginActivity.this, logmodel.token,
                                new UserModel.UserInfoLoginStatus() {
                                    @Override
                                    public void conver(UserModel model) {
                                        if (model != null) {
                                            JMessageManage.login(model.invite_code, model.pass_word);
                                        }
                                        if (mDialog != null) {
                                            mDialog.dismiss();
                                        }
                                        // 判断是否设置邀请码
                                        if (logmodel.has_invited == 0) {
                                            startActivity(new Intent(LoginActivity.this, InvitedActivity.class));
                                        } else {
                                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                            startActivity(intent);
                                        }
                                        TanZiApplication.code = null;
                                        finish();
                                    }
                                });
                    } else {
                        if (mDialog != null) {
                            mDialog.dismiss();
                        }
                    }
                }
            });
        }
    }

    private void login() {
        mDialog = DialogCreator.createLoadingDialog(this, "正在登陆");
        mDialog.show();
        String name = etname.getText().toString();
        String pwd = etpwd.getText().toString();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(LoginActivity.this, getResources().getString(R.string.no_account_input), Toast.LENGTH_LONG).show();
        } else if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(LoginActivity.this, getResources().getString(R.string.no_pwd_input), Toast.LENGTH_LONG).show();
        } else {
            LoginResultModel.LoginWithPwd(this, name, pwd, new LoginResultModel.LoginStatus() {
                @Override
                public void conver(LoginResultModel logmodel) {

                    if (null != logmodel) {
                        UserModel.getUserInfo(LoginActivity.this, logmodel.token,
                                new UserModel.UserInfoLoginStatus() {
                            @Override
                            public void conver(UserModel model) {
                                JMessageManage.login(model.invite_code, model.pass_word);
                                if (mDialog != null) {
                                    mDialog.dismiss();
                                }
                                // 判断是否设置邀请码
                                if (logmodel.has_invited==0){
                                    startActivity(new Intent(LoginActivity.this,InvitedActivity.class));
                                } else {
                                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
                                }
                                finish();
                            }
                        });

                    }else {
                        if (mDialog != null) {
                            mDialog.dismiss();
                        }
                    }
                }
            });
        }

    }

    @OnClick({R.id.tvcode, R.id.tvregist, R.id.tvlogin, R.id.ivfinish,R.id.tvwelogin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvcode:
                CodeLoginActivity.into(this);
                finish();
                break;
            case R.id.tvregist:
                RegistActivity.into(this);
                break;
            case R.id.tvlogin:
                login();
                break;
            case R.id.ivfinish:
                finish();
                break;
                case R.id.tvwelogin:
                    wxLogin();
                break;
        }
    }

    public void wxLogin() {
        mDialog = DialogCreator.createLoadingDialog(this, "正在登陆");
        mDialog.show();
        final SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "diandi_wx_login";
        TanZiApplication.api.sendReq(req);
        TanZiApplication.mUserModel = null;
    }


}
